package app.server.project.main.service.db.dao.user;

import app.server.project.main.service.db.entity.user.UserSignEntity;
import app.server.project.main.service.db.mapper.user.UserSignTimeMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class UserSignDao extends ServiceImpl<UserSignTimeMapper, UserSignEntity> {

    @Cacheable(key = "#userId", cacheNames = "app.server.project.main.service.db.dao.UserSignDao")
    public List<UserSignEntity> getLast30(long userId, long days) {
        Page<UserSignEntity> page = new Page<>(1, days);
        var ret = this.page(page, Wrappers.<UserSignEntity>lambdaQuery()
                .orderByDesc(UserSignEntity::getSignTime)
                .eq(UserSignEntity::getUserId, userId));
        var dbs = new ArrayList<>(ret.getRecords());
        dbs.forEach(userSignEntity -> {
            userSignEntity.setSynDb(true);
        });
        return dbs;
    }

    public long countByUserId(long userId) {
        return this.count(Wrappers.<UserSignEntity>lambdaQuery().eq(UserSignEntity::getUserId, userId));
    }

    public List<UserSignEntity> getByUserId(long userId) {
        Page<UserSignEntity> page = new Page<>(1, 10 * 365);
        var ret = this.page(page, Wrappers.<UserSignEntity>lambdaQuery()
                .orderByDesc(UserSignEntity::getSignTime)
                .eq(UserSignEntity::getUserId, userId));
        return new ArrayList<>(ret.getRecords());
    }
}
